%*************************************************************************************
%      FileName        : con_encod.m
%      Author          : Yao Gang
%      Date            : 2003.12.20
%      Version         : 1.0
%      E-mail          : Y.Gang@ed.ac.uk
%      Company         : SLI Group Of University Of Edinburgh
%      Description     : Matalab Code Of Convolutional Encoder
%
%                     ! ! !
%                    / o o \
%                   {   >   }
%                    \  ~  /                  <-nobond:yaogang-self_portrait
%                   __|   |__
%***************************************************************************************

%***************************************************************************************
%trellis description of the the encoder
%***************************************************************************************

function encoderbits = con_encod(encoder,inputstreams);

%***************************************************************************************
%encoder : Trellis Data Structure
%***************************************************************************************

%encoder.numInputSymbols
%encoder.numOutputSymbols
%encoder.numStates
%encoder.nextStates
%encoder.outputs

%***************************************************************************************
%inputstreams : uncoded bits stremas
%***************************************************************************************
%inputstreams:encodertime x X k matrix

%***************************************************************************************
%Generate the Initialization Table Symbol
%***************************************************************************************
k = log2(encoder.numInputSymbols);
n = log2(encoder.numOutputSymbols);

[num_iteration,null] = size(inputstreams);

if null~= k
 disp('Sorry,input stream not consistent with the trellis structure!');
end

encoderbits     = zeros(num_iteration,n);

%***************************************************************************************
%Initial State  ----> all the states is one position shift from his hardware representation
%***************************************************************************************
register        = 1;


%***************************************************************************************
%Encode Process Begin
%***************************************************************************************
for i = 1 : num_iteration
  %output symbols
  temp = encoder.outputs(register,binarray2dec(inputstreams(i,:) + 1));

  encoderbits(i,:) = dec2binarray(temp,n);

  %register update
  register = encoder.nextStates(register, binarray2dec(inputstreams(i,:) + 1 )); 
end
